package com.wq.controller;

import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.common.PageResult;
import com.wq.common.Result;
import com.wq.dto.CategoryDeleteDTO;
import com.wq.dto.CategoryPagDTO;
import com.wq.entity.Category;
import com.wq.service.CategoryService;
import com.wq.vo.CategoryListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;




    @PostMapping("list")
    public PageResult<Category> list(@RequestBody CategoryPagDTO categoryPagDTO){
        log.info("{}",categoryPagDTO);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        IPage<Category> pageInfo = new Page<>(categoryPagDTO.getCurrent(),categoryPagDTO.getSize());
        LambdaQueryWrapper <Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(categoryPagDTO.getCategoryName()!=null,Category::getName,categoryPagDTO.getCategoryName());
        wrapper.between(categoryPagDTO.getStartDate()!=null,Category::getCreateTime,categoryPagDTO.getStartDate(),categoryPagDTO.getEndDate());
        IPage<Category> page = categoryService.page(pageInfo, wrapper);
        String s = stopWatch.prettyPrint();
        System.out.println(s);
        return PageResult.success(page);
    }
    @PostMapping("add")
    public Result<Object> add(@RequestBody Category category){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        categoryService.save(category);
        stopWatch.stop();
        String s = stopWatch.prettyPrint();
        System.out.println(s);
        return Result.success();
    }
    @PostMapping("delete")
    public void delete(@RequestBody CategoryDeleteDTO categoryDeleteDTO){
        categoryService.removeById(categoryDeleteDTO.getCategoryId());
    }

    @PostMapping("select/list")
    public Result<List<CategoryListVO>> selectList(){
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<CategoryListVO> list = categoryService.selectList();
        stopWatch.stop();
        String s = stopWatch.prettyPrint();
        System.out.println(s);
        return Result.success(list);
    }




}
